<?php

/**
 * Contains a class to display the search page.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */

/** @uses encode For safe output of user-provided input. */
require_once 'function_encode.php';
 
/** @uses paginate To create pagination links. */
require_once 'function_paginate.php';

/** @uses valueOf For safe handling of arrays. */
require_once 'function_valueof.php';

/**
 * A class to display /search.php.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class PageSearch extends Page
{
    /**
     * Contains the active search term(s).
     *
     * @access public
     * @var string
     */
    public $query;
    
    /**
     * Contains the current page.
     *
     * @access public
     * @var int
     */
    public $page;
    
    /**
     * Constructor function.  Calls the parent constructor and sets the title.
     */
    public function __construct()
    {
        parent::__construct();
        
        /** Gets the query text. */
        $this->query = valueOf($_GET, 'query');
        
        /** Gets the current page number. */
        $this->page = valueOf($_GET, 'page');
        if ($this->page == '') {
            $this->page = 1;
        }
        
        /** Gets the current zip code. */
        if (valueOf($_GET, 'zip') != '') {
            $zip = $this->db->getZipByZip($_GET['zip']);
            if ($zip) {
                $_SESSION['zip'] = $_GET['zip'];
                $this->user->zip = $zip;
            } else {
                $_SESSION['zip'] = D_ZIP;
                $this->user->zip = $this->db->getZipByZip(D_ZIP);
            }
        }
        
        /** Gets the search radius. */
        if (valueOf($_GET, 'radius') != '') {
            $_SESSION['radius'] = valueOf($_GET, 'radius');
        }
        if (valueOf($_SESSION, 'radius') == '') {
            $_SESSION['radius'] = valueOf($this->radius, 0);
        }
        
        /** Sets the page title. */
        $title = 'Search';
        if ($this->query != '') {
            $title .= ': ' . encode($this->query);
        }
        parent::setTitle($title);
    }
    
    /**
     * Function to get the HTML for printing the pagination.
     *
     * @param int $totalResults The total number of results from the query.
     *
     * @access private
     * @return string
     */
    private function _getPagination($totalResults)
    {
        return paginate($this->page, $totalResults, STORES_PER_PAGE);
    }
    
    /**
     * Function to display the page.
     *
     * @access public
     * @return null
     */
    public function display()
    {
        parent::printHeader();
        if (valueOf($_GET, 'query') == '') {
            printError('Please enter a search term.', 1);
        } else {
            $storeInfo    = $this->db->getStoresByQuery(
                $this->query, $this->page, $this->user->zip->latitude, 
                $this->user->zip->longitude, $_SESSION['radius']
            );
            $totalResults = $storeInfo['totalResults'];
            $stores       = $storeInfo['stores'];
            if (count($stores) == 0) {
                printError('No results found.', 1);
                return null;
            } else {
                $pagination = $this->_getPagination($totalResults);
                $rangeMin   = ($this->page - 1) * STORES_PER_PAGE + 1;
                $rangeMax   = min($totalResults, $rangeMin + STORES_PER_PAGE - 1);
                $rangeHtml  = '  <p>Displaying ' . $rangeMin . '-' . $rangeMax
                    . ' of ' . $totalResults . ' results.</p>' . "\n";
                echo $rangeHtml;
                echo $pagination,
                   ' <ul class="stores">', "\n";
                foreach ($stores as $store) {
                    $store->printListItem();
                }
                echo ' </ul>', "\n",
                   $pagination,
                   ' <h2>These Stores on a Map</h2>', "\n";
                $map = new Map($stores);
                $map->display();
            }
        }
        parent::printFooter();
    }
}

?>